#include "xmlfile.h"

XMLFile::XMLFile()
{
metatag = false;
count = 0;
}

XMLFile::~XMLFile()
{
}

bool XMLFile::startElement( const QString & ns,
                            const QString & localName,
                            const QString & qName,
                            const QXmlAttributes & attributes )
{

    if ( !metatag && true != qName.contains("Workbook") )
    {
    errorStr = QObject::tr("The file is not a MS XML Speadsheet file.");
    return false;
    }

    if ( qName == "Workbook" ||
         localName == "Workbook")
    {
    metatag = true;
    }

    if (ns != "urn:schemas-microsoft-com:office:spreadsheet")
    {
    return true;
    }


    if ( qName == "Worksheet" ||
         localName == "Worksheet" )
    {
        if ( !attributes.value("ss:Name").isNull() &&
             !attributes.value("ss:Name").isEmpty() )
        {
        l << attributes.value("ss:Name");
        }
        else if ( !attributes.value("Name").isNull() &&
                  !attributes.value("Name").isEmpty() )
        {
        l << attributes.value("Name");
        }
    }

return true;
}

bool XMLFile::endElement(const QString & ns,
                         const QString & localName,
                         const QString & qName )
{
    if (ns != "urn:schemas-microsoft-com:office:spreadsheet")
    {
    return true;
    }

    if ( qName == "Worksheet" ||
         localName == "Worksheet" )
    {
    count += 1;
    }    
return true;
}

bool XMLFile::fatalError(const QXmlParseException & exception)
{
errorStr = QObject::tr( "Parse error at line %1, column %2:\n %3")
                           .arg(exception.lineNumber())
                           .arg(exception.columnNumber())
                           .arg(exception.message() );
return false;
}

QString XMLFile::errorString() const
{
return errorStr;
}

QStringList XMLFile::return_data()
{
return l;
}
